/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.warehouse.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.modules.warehouse.entity.MaterialReport;
import com.thinkgem.jeesite.modules.warehouse.service.MaterialReportService;

/**
 * MaterialReportController
 * 
 * @author arg
 * @version 2017-03-30
 */
@Controller
@RequestMapping(value = "${adminPath}/warehouse/materialReport")
public class MaterialReportController extends BaseController {

	@Autowired
	private MaterialReportService materialReportService;

	@ModelAttribute
	public MaterialReport get(@RequestParam(required = false) String id) {
		MaterialReport entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = materialReportService.get(id);
		}
		if (entity == null) {
			entity = new MaterialReport();
		}
		return entity;
	}

	@RequiresPermissions("warehouse:materialReport:view")
	@RequestMapping(value = { "list", "" })
	public String list(MaterialReport materialReport, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<MaterialReport> page = materialReportService.findPage(new Page<MaterialReport>(request, response),
				materialReport);
		model.addAttribute("page", page);
		return "modules/warehouse/materialReportList";
	}

	@RequiresPermissions("warehouse:materialReport:view")
	@RequestMapping(value = "form")
	public String form(MaterialReport materialReport, Model model) {
		model.addAttribute("materialReport", materialReport);
		return "modules/warehouse/materialReportForm";
	}

	@RequiresPermissions("warehouse:materialReport:view")
	@RequestMapping(value = "export", method = RequestMethod.POST)
	public String exportFile(HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes, MaterialReport materialReport) {
		try {
			String fileName = "MaterialReport" + DateUtils.getDate("yyyyMMddHHmmss")
					+ ".xlsx";
			Page<MaterialReport> page = materialReportService.findPage(
					new Page<MaterialReport>(request, response, -1),
					materialReport);
			new ExportExcel("MaterialReport", MaterialReport.class)
					.setDataList(page.getList()).write(response, fileName)
					.dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "Export Material Report failed：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath()
				+ "/warehouse/materialReport/?repage";

	}

}